$(document).ready(function(){
   

  if(navigator.appName == "Microsoft Internet Explorer"){ 
    alert("ie");
  }
  if ("window", $( window ).width() < 801){
    jQuery(".deviceInfos, .deviceInfosIns").addClass("hidden");
  } 
  var hgt = $(".gallery-content .gallery960").height();
  
  window.parent.postMessage(hgt,"*"); 
  var offsetpage = $(".gallery-pagination").offset().top; 
  
  jQuery(window).on("resize", function(event){
    resizeIframe();
  });
  
  //temporary fix for removing  All at filter 
  $(".filters span[key='all']").parent('li').remove();

  //fix for disabling 0 device from filter data
  if($(".filterData").text() == "(0)"){
    $(".filterData:contains('0')").parent().attr("style","text-decoration:line-through!important;");
  }

  $("select").select2();
  //console.log(navigator);
  // Hide All in initial
  jQuery("select.brandsorter option:contains('All brands')").attr("disabled", "disabled");
 
  //. . . . .  Filter toggle
  /* added by ian for reset */
 $(".gallery-filter .filters a[href='#currentfilter']").click(function(){
    $(this).children("span").toggleClass("expand");
    $("#currentfilter").slideToggle("slow");
    return false;
  });
 


  jQuery("#reset-price").click(function(e) {
      orderDevices(e,jQuery("ul#price li a").last()); 
      sortGallery();
      updatePageIndicator(); 
      jQuery("#current-filter-price-container").hide();
      hideReset();
      jQuery("#label-price, #price").show();
    

  });
  
  jQuery("#reset-brand").click(function(e) {
    //orderDevices(e,jQuery("ul#price li a").last());
    jQuery(".brand .select2-chosen").text("All brands");
    jQuery("select.brandsorter").prop('selectedIndex', 0);
    jQuery("select.brandsorter option").removeAttr('disabled');
    jQuery("select.brandsorter option:first-child").attr("disabled", "disabled");

    orderDevices(e,jQuery("div.sorter select"));
    updatePageIndicator(); 
    jQuery("#current-filter-brand-container").hide();
    hideReset();
    sortGallery();

    //jQuery("#label-current-filter, #currentfilter, #current-filter-price-container").hide();
  }); 
  
  
      $(".gallery-filter a[href='#filter']").click(function(){
            //console.log("Click .gallery-filter a[href='#filter']");
            //console.log("Click #gallery-filter-by-collapse");
            $(this).children("span").toggleClass("expand");
            $(".gallery-filter .filters").slideToggle("slow");
            return  false;
        });
    
        $(".gallery-filter .filters a[href='#price']").click(function(){
            //console.log("Click .gallery-filter .filters a[href='#price']");
            $(this).children("span").toggleClass("expand");
            // I want jquery handle the toggle
            $("#price").slideToggle("slow");
            return false;
        });

        //
        // $("#phoneGallery .wizard").css("margin-top","60px");


  //fix for pageination toolbar
  $(".gallery-pagination").css("margin-left","0px");
   //fix for pageination toolbar
  //$("#price a").css("text-decoration","underline");
  $(".filterData:contains('0')").parent().attr("style","text-decoration:line-through!important;");
  if($(".filterData").text() == "(0)"){
   
    $("span.filterData").find(":contains('(0)')").parent().attr("style","text-decoration:line-through!important;");
  }
  addHeader();  //this is gallery header
  

  var current_pos = getCookie("current_pos");
  if (current_pos > 1){
      //alert(current_pos + "TRUE");
      if(current_pos=="2") {
          // update selected device
          markSelectedDevice();

          jQuery("div.wizard a:nth-of-type(1)").attr("class","current done");
          jQuery("div.wizard a:nth-of-type(2)").attr("class","current");
          jQuery("div.wizard a:nth-of-type(3), div.wizard a:nth-of-type(4), div.wizard a:nth-of-type(5)").removeAttr("href");
      
          jQuery('.marg20Left').css("position","absolute");
          jQuery('.marg20Left').css("margin-top","0px");
      
      //edit by JOHN CRUZ 10/23/2014
      jQuery(window).on("resize", function(event){
      if ("window", $( window ).width() < 801){
        jQuery(".deviceInfos, .deviceInfosIns").removeClass("block2");
        jQuery(".deviceInfos, .deviceInfosIns").addClass("hidden");
      } else {
        jQuery(".deviceInfos, .deviceInfosIns").addClass("hidden");
        jQuery(".deviceInfosIns").addClass("block2");
      }
      resizeIframe();
      });
      
          jQuery('.deviceInfosIns').css("margin-top","-70px","important");
          jQuery('.deviceInfosIns').css("margin-left","4px","important");
          jQuery('.gallery-filter').css("width","210px");
          jQuery('.deviceInfosIns').css("width","210px");
          //jQuery(".deviceInfos").addClass("hidden");
          //jQuery(".deviceInfosIns").addClass("block2");
          
          var mode = getCookie("mode");
          var gadget = getCookie("gadget");
          var price = getCookie("price");
          
          if(mode!="edit"){      
                 jQuery(".promoBox, .commitmentBox, .prepaidBox, .profileBox, .storeBox").removeClass("active");
                 jQuery('#pname, #pemail, #pdob, #padd').hide();
                 jQuery('#promo, #commitmentBox, #mobtel, #globestore').text("None");
                 jQuery('#gadget').text(gadget + " - " + price);
          }
    }
    if(current_pos=="3") {
          // update selected device
          markSelectedDevice();

          jQuery("div.wizard a:nth-of-type(1), div.wizard a:nth-of-type(2)").attr("class","current done");
          jQuery("div.wizard a:nth-of-type(3)").attr("class","current");
          jQuery("div.wizard a:nth-of-type(4), div.wizard a:nth-of-type(5)").removeAttr("href");
      
      //edit by JOHN CRUZ 10/23/2014
      jQuery(window).on("resize", function(event){
      if ("window", $( window ).width() < 801){
        jQuery(".deviceInfos, .deviceInfosIns").removeClass("block2");
        jQuery(".deviceInfos, .deviceInfosIns").addClass("hidden");
      } else {
        jQuery(".deviceInfos, .deviceInfosIns").addClass("hidden");
        jQuery(".deviceInfosIns").addClass("block2");
      }
      resizeIframe();
      });
      
          jQuery('.marg20Left').css("position","absolute");
          jQuery('.marg20Left').css("margin-top","0px");
          jQuery('.deviceInfosIns').css("margin-top","-70px","important");
          jQuery('.deviceInfosIns').css("margin-left","9px","important");
          jQuery('.gallery-filter').css("width","210px");
          jQuery('.deviceInfosIns').css("width","210px");
          //jQuery(".deviceInfos").addClass("hidden");
          //jQuery(".deviceInfosIns").addClass("block2");

          var mode = getCookie("mode");
          var gadget = getCookie("gadget");
          var price = getCookie("price");
          var promo = getCookieR("promo");
          var commitmentBox = getCookieR("commitmentBox");
      
          if(mode!="edit"){      
              jQuery(".prepaidBox, .profileBox, .storeBox").removeClass("active");
              jQuery('#pname, #pemail, #pdob, #padd').hide();
              jQuery('#mobtel, #globestore').text("None");
              jQuery('#gadget').text(gadget + " - " + price);
              jQuery('#promo').html(promo);
              jQuery('#commitmentBox').text(commitmentBox);
          }
      }
      if(current_pos=="4") {
          jQuery("div.wizard a:nth-of-type(1), div.wizard a:nth-of-type(2),div.wizard a:nth-of-type(3)").attr("class","current done");
          jQuery("div.wizard a:nth-of-type(4)").attr("class","current");
          jQuery("div.wizard a:nth-of-type(5)").removeAttr("href");
      
       //edit by JOHN CRUZ 10/23/2014
      jQuery(window).on("resize", function(event){
      if ("window", $( window ).width() < 801){
        jQuery(".deviceInfos, .deviceInfosIns").removeClass("block2");
        jQuery(".deviceInfos, .deviceInfosIns").addClass("hidden");
      } else {
        jQuery(".deviceInfos, .deviceInfosIns").addClass("hidden");
        jQuery(".deviceInfosIns").addClass("block2");
      }
      resizeIframe();
      });
      
          jQuery('.marg20Left').css("position","absolute");
          jQuery('.marg20Left').css("margin-top","0px");
          jQuery('.deviceInfosIns').css("margin-top","-70px","important");
          jQuery('.deviceInfosIns').css("margin-left","9px","important");
          jQuery('.gallery-filter').css("width","210px");
          jQuery('.deviceInfosIns').css("width","210px");
          //jQuery(".deviceInfos").addClass("hidden");
          //jQuery(".deviceInfosIns").addClass("block2");

          var mode = getCookie("mode");
          var gadget = getCookie("gadget");
          var price = getCookie("price");
          var promo = getCookieR("promo");
          var commitmentBox = getCookieR("commitmentBox");
          var mobtel = getCookieR("mobtel");
          
          if(mode!="edit"){      
            jQuery(".profileBox, .storeBox").removeClass("active");
            jQuery('#pname, #pemail, #pdob, #padd').hide();
            jQuery('#globestore').text("None");
            jQuery('#gadget').text(gadget + " - " + price);
            jQuery('#promo').html(promo);
            jQuery('#commitmentBox').text(commitmentBox);
            if(mobtel=="0"){jQuery('#mobtel').text("none");} else {jQuery('#mobtel').text(mobtel);}
          }
      }
      if(current_pos=="5" || current_pos=="6") {
          jQuery("div.wizard a:nth-of-type(1),div.wizard a:nth-of-type(2),div.wizard a:nth-of-type(3),div.wizard a:nth-of-type(4)").attr("class","current done");
          jQuery("div.wizard a:nth-of-type(5)").attr("class","width100 current");
        
       //edit by JOHN CRUZ 10/23/2014
      jQuery(window).on("resize", function(event){
      if ("window", $( window ).width() < 801){
        jQuery(".deviceInfos, .deviceInfosIns").removeClass("block2");
        jQuery(".deviceInfos, .deviceInfosIns").addClass("hidden");
      } else {
        jQuery(".deviceInfos, .deviceInfosIns").addClass("hidden");
        jQuery(".deviceInfosIns").addClass("block2");
      }
    
      });
      
          jQuery('.marg20Left').css("position","absolute");
          jQuery('.marg20Left').css("margin-top","0px");
          jQuery('.deviceInfosIns').css("margin-top","-70px","important");
          jQuery('.deviceInfosIns').css("margin-left","9px","important");
          jQuery('.gallery-filter').css("width","210px");
          jQuery('.deviceInfosIns').css("width","210px");
          //jQuery(".deviceInfos").addClass("hidden");
          //jQuery(".deviceInfosIns").addClass("block2");

          var mode = getCookie("mode");
          var gadget = getCookie("gadget");
          var price = getCookie("price");
          var promo = getCookieR("promo");
          var commitmentBox = getCookieR("commitmentBox");
          var mobtel = getCookieR("mobtel");
          var username = getCookieR("username");
          var eadd = getCookieR("eadd");
          var bday = getCookieR("bday");
          var address = getCookieR("address");
		  var area = getCookieR("area");
		  var town = getCookieR("town");
		  var province = getCookieR("province");
		  var postalcode = getCookieR("postalcode");
          jQuery("p.pnone").hide();
          if(mode!="edit"){      
            jQuery(".storeBox").removeClass("active");
            jQuery("p.pnone").hide();
            jQuery('#globestore').text("None");
            jQuery('#gadget').text(gadget + " - " + price);
            jQuery('#promo').html(promo);
            jQuery('#commitmentBox').text(commitmentBox);
            if(mobtel=="0"){jQuery('#mobtel').text("none");} else {jQuery('#mobtel').text(mobtel);}
            jQuery('#name').text(username);
            jQuery('#email').text(eadd);
            jQuery('#dob').text(bday);
			
			if(province=="Metro Manila") 
				var address = address + ', ' + area + ', ' + town + ', ' + province + ', ' + postalcode;
			else
				var address = address + ', ' + town + ', ' + province + ', ' + postalcode;
				
            jQuery("#pname, #pemail, #pdob, #padd").show();
          }
            
        
      }
    /*
    if(current_pos=="6") {
    
      jQuery('.marg20Left').css("position","absolute");
        jQuery('.marg20Left').css("margin-top","0px");
      jQuery('.deviceInfosIns').css("margin-top","-70px","important");
      jQuery('.deviceInfosIns').css("margin-left","9px","important");
      jQuery('.gallery-filter').css("width","210px");
      jQuery('.deviceInfosIns').css("width","210px");
      jQuery(".deviceInfos").addClass("hidden");
      jQuery(".deviceInfosIns").addClass("block2");

      var mode = getCookie("mode");
      var gadget = getCookie("gadget");
      var price = getCookie("price");
      var promo = getCookieR("promo");
      var commitmentBox = getCookieR("commitmentBox");
      var mobtel = getCookieR("mobtel");
      var username = getCookieR("username");
      var eadd = getCookieR("eadd");
      var bday = getCookieR("bday");
      var address = getCookieR("address");
      var globestore = getCookieR("globestore");
      
      if(mode!="edit"){      
      jQuery(".pnone").hide();
      jQuery('#globestore').text("None");
      jQuery('#gadget').text(gadget + " - " + price);
      jQuery('#promo').html(promo);
      jQuery('#commitmentBox').text(commitmentBox);
      jQuery('#mobtel').text(mobtel);
      jQuery('#name').text(username);
      jQuery('#email').text(eadd);
      jQuery('#dob').text(bday);
      jQuery('#add').text(address);
      jQuery('#globestore').text(globestore);
      jQuery("#pname, #pemail, #pdob, #padd").show();
      }
      } */
  } else {
    //alert(current_pos + "FALSE");
    jQuery("div.wizard a:nth-of-type(1)").attr("class","current");
    jQuery("div.wizard a:nth-of-type(1), div.wizard a:nth-of-type(2), div.wizard a:nth-of-type(3), div.wizard a:nth-of-type(4), div.wizard a:nth-of-type(5)").removeAttr("href");
  
  //edit by JOHN CRUZ 10/23/14
  jQuery(window).on("resize", function(event){
    if ("window", $( window ).width() < 801){
      jQuery(".deviceInfos").removeClass("block2");
      jQuery(".deviceInfos, .deviceInfosIns").addClass("hidden");
     // console.log("640 x 801");
    } else {
      jQuery(".deviceInfos").removeClass("hidden");
      jQuery(".deviceInfos").addClass("block2");
    }
    resizeIframe();
  });
  
  } 
  window.brand = $(".brand .select2-chosen").text();   
  window.allproducts = []; //wierd nito unneccessary 
  $("ul#products-list li.item").each(function(index, value){     
    // window.allproducts.push($(this).find("h3.product-name").text());
 
    //console.log(value);   
     window.allproducts.push(value);
  });

  jQuery("li.item").addClass("_display");
  /*  filter price adding marker to  prices */  

  $("ul#price li a").eq(0).find("span.price").eq(0).addClass("range0"); // this is hidden li for all 
  $("ul#price li a").eq(1).find("span.price").eq(0).addClass("range1");
  $("ul#price li a").eq(2).find("span.price").eq(0).addClass("range2");
  $("ul#price li a").eq(3).find("span.price").eq(0).addClass("range3");
  $("ul#price li a").eq(4).find("span.price").eq(0).addClass("range4");
  $("ul#price li a").eq(5).find("span.price").eq(0).addClass("range5");
  /* filter price */
  
  if( $("div#phoneGallery").is(":visible") ){
    displayDefaultDevices();
  }
  
  /* Load More */
  $("a#loadmoreitems").click(function(){
    updatePageIndicator();  
    //lastPostFunc(); 
    var x = $("p.page-indicator").text();
    if($("._display[style='display: none;']").length == 0 ){
        $("a#loadmoreitems").hide();
    }
    if( $("._display[style='display: none;']").length == 0  || $("._display[style='display: block;']").length  == $("._display").length || $("._display[style='display: block;']").length  == x.replace("items", "").replace(/\s/g, "")){
          $("a#loadmoreitems").hide();   $("a#loadmoreitems").attr("style","display:none");
    } 
    hgt = $(".gallery-content .gallery960").height();
    window.parent.postMessage(hgt,"*");  
  });
  /* Load More */
  
  /* order devices events for mobile */
    

  /* Order devices events  */
  /* mod by ian for reset 101114 */
  $("ul#price li dl dd a  , ul#price li a , .select2-results li , select2-drop").click(function(e){       
    e.preventDefault(); // prevent a tags
    /*if($(this).find("span").hasClass("price") && $(this).sibling().text() == "(0)" ){
      console.log( $(this).parent().html() );
      console.log( $("._display").length );
      $("ul.gallery-products").html(window.allproducts); return 1;
    }*/
    if($(this).find(".filterData").text() == "(0)"){
      return 1;
      $(this).attr("style","text-decoration:line-through;");
      $("ul.gallery-products").html(window.allproducts);  
      $(".item").removeClass("_display");  updatePageIndicator();  $("li.item").hide(); return 1;     
    }
    if(jQuery(this).text()!="All prices") {
      showReset(jQuery(this), "price"); 
      jQuery("#label-price, #price").hide(); // hide filter price
    }
    orderDevices(e,$(this));      
    updatePageIndicator(); 
    sortGallery(); 
  });
   
  $(".sort-by li , .orderby select option").click(function(e){
    e.preventDefault(); // prevent a tags
    orderDevices(e,$(this));      
    updatePageIndicator(); 
    sortGallery(); 
  });
  
 $(" .orderby select").change(function(e){  
      orderDevices(e,$(this));      
      updatePageIndicator(); 
      sortGallery(); 
 }); 

  $("select.brandsorter").change(function(e){     
    // console.log("selected brand: " + jQuery("option:selected", this).text());
    selectedBrand = jQuery("option:selected", this);
    orderDevices(e,$(this)); 
    updatePageIndicator();  
    //sortGallery();  
    jQuery("option", this).removeAttr("disabled");
    jQuery("option:selected", this).attr('disabled','disabled');
    //selectedBrand = jQuery(".brand .select2-chosen");
    //console.log("selectedBrand: " + selectedBrand.text());
    if(selectedBrand.text()!="All brands") {
      showReset(selectedBrand, "brand");
    } else {
      jQuery("#current-filter-brand-container").hide();
      hideReset();
    }
    //orderDevices(e,$(this),price); 
      
  });
 
  $("select.mprice ,select.mbrand").change(function(e){ 
      mobileOrderDevices(e,$(this)); 
      updatePageIndicator();   
      //alert("ssdr");  
      //jQuery("option", this).removeAttr('disabled');
      //jQuery("option:selected", this).attr('disabled','disabled');
      //selectedBrand = jQuery(".brand .select2-chosen");
      //console.log("selectedBrand: " + selectedBrand.text());     
      //orderDevices(e,$(this),price); 
      
  });  
   
  $(".sort-by ul li a").click(function(){
   
    $(".sort-by ul.tabs").find("li.active").removeClass("active");
    $(this).parent().addClass("active");
  });

  $(".orderby select").change(function(e){     
       orderDevices(e,$(this),price);
       updatePageIndicator();  
       sortGallery();
  });  
  
  /* End Order devices events  */ 

  /* From gallery.php : brands dropdown */  
  jQuery("#brand-newcontainer").html(jQuery("#brand-container").html());

  var tabs = jQuery('#brand-newcontainer .tabs');

  jQuery('a', tabs).on( "click", function(e){
    e.preventDefault();
    jQuery(this).parent().addClass('active').siblings().removeClass('active');
  });

  jQuery('#brand-newcontainer .sorter a.tabs').on( "click", function(e) {
      e.preventDefault();
      var dataList = jQuery(jQuery(this).attr('href').toString());
      jQuery('#brand-newcontainer .sorter .data-list').filter('.active').hide().removeClass('active');
      dataList.show().addClass('active');
  });

  jQuery('#brand-newcontainer .data-list a').on( "click", function(e) {
    var ths = jQuery(this),pos = $this.parent().position().top;
    //$this.parents('.data-list').css('top', -pos).hide().removeClass('active');
    //$this.parents('.sorter').find('a.tabs').contents()[0].nodeValue = $this.text();   //----> Hi all '$this'  is replaced by '$(ths)' bec of ie8-pogiproblems
    $(ths).parents('.data-list').css('top', -pos).hide().removeClass('active');
    $(ths).parents('.sorter').find('a.tabs').contents()[0].nodeValue = $(ths).text();   
  });  

  $('#btn_top').on("click",function(){
    scrollFrameHeight('0');
  });
  
  updatePageIndicator();  
  sortGallery();
  /* From gallery.php : brands dropdown */
  if(/gallery/.test(self.location.href)){ 
   var mode = getCookie("mode");
   if(mode=="edit"){
    jQuery('.deviceInfos').hide();
    jQuery('.deviceInfosIns').show();

     /*
       var gadget = getCookie("gadget") + " - P" + getCookie("price");
       var promo = getCookieR("promo");
       var commitment = getCookieR("commitmentBox");
       var prepaid = getCookie("mobtel");
    */
   
    jQuery('.marg20Lef